var express = require('express');
var router = express.Router();
//引入连接数据库模块
const connection = require('./conn');

//连接数据库
connection.connect(() => {
    console.log('连接数据库成功！');
});
/* 接收登录用户请求 */
router.post('/checklogin', (req, res) => {
    //接收用户名和密码
    let {
        username,
        password
    } = req.body;
    console.log(username, password);
    //去数据库查询是否存在这个用户名和密码
    const sql = `SELECT * FROM users WHERE username='${username}' AND password='${password}'`;
    connection.query(sql, (err, data) => {
        if (err) {
            throw err;
        } else {
            res.send(data);
        }
    });
});

/* 接收用户列表请求 */
router.get('/getuserlist', (req, res) => {
    const sql = 'SELECT * FROM users';
    connection.query(sql, (err, data) => {
        if (err) {
            throw err;
        } else {
            res.send(data)
        }
    })
})

module.exports = router;